package com.starblink.comment.adapter

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.dp2px
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.modules.mine.MineManager
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.android.basic.util.post.PostUtils
import com.starblink.basic.ext.load
import com.starblink.basic.util.CommUtils
import com.starblink.comment.bean.PostComment
import com.starblink.comment.databinding.ItemCommentBinding

class CommentItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleRes) {

    companion object {
        const val TAG = "CommentItemView"
    }

    private val likeImg = com.starblink.basic.style.R.mipmap.video_like
    private val unlikeImg = com.starblink.basic.style.R.mipmap.unlike
    private val avatar = com.starblink.basic.style.R.mipmap.mine_default_avatar

    lateinit var comment: PostComment

    val binding: ItemCommentBinding = ItemCommentBinding.inflate(inflater, this, true)

    init {
    }

    fun bindProduct(
        comment: PostComment,
        isReply: Boolean = false
    ) {
        if (comment != null) {
            this.comment = comment
            binding.ivUser.run {
                if (comment.user != null) {
                    comment.user!!.accountF.headImage?.let {
                        load(
                            it,
                            placeholder = CommUtils.getDrawable(avatar),
                            error = CommUtils.getDrawable(avatar)
                        )
                    }
                }
            }
            val accountDel = comment.user?.accountF?.deleted ?: false
            val commentDel = comment.deleted
            binding.tvLikeNum.text =
                if (comment.likeCount == 0) "" else comment.likeCount.toString()

            if (accountDel) {
                binding.tvName.text =
                    CommUtils.getString(com.starblink.basic.style.R.string.account_deleted)
            } else {
                if (isReply && comment.replyUser != null) {
                    val lp: LayoutParams =
                        binding.ivUser.layoutParams as LayoutParams
                    lp.leftMargin = dp2px(30f)
                    binding.ivUser.layoutParams = lp

                    binding.tvName.buildSpannableString {
                        addText("${comment.user?.accountF?.userName ?: ""}") {
                        }
                        addText(" @${comment.replyUser!!.accountF.userName}") {
                            setColor(Color.parseColor("#CFB58E"))
//                            {
//                                comment.replyUser?.accountF?.let { it1 ->
//                                    MineManager.navigationToOtherUserPersonHome(
//                                        context,
//                                        it1.id,
//                                        it1.deleted,
//                                        it1.username
//                                    )
//                                }
//                            }
                        }
                        addText(" · ${PostUtils.getTimeDiffDetail(comment.publishTime.toString())}") {
                        }

                    }
                } else {
                    binding.tvName.text =
                        "${comment.user?.accountF?.userName ?: ""} · ${
                            PostUtils.getTimeDiffDetail(
                                comment.publishTime.toString()
                            )
                        }"
                }
            }
            binding.tvContent.text =
                if (commentDel) CommUtils.getString(com.starblink.basic.style.R.string.CommentHasBeen) else comment.content

            if (commentDel) {
                binding.tvContent.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray400))
            }

            if (comment.liked) {
                binding.ivLike.setImageResource(likeImg)
            } else {
                binding.ivLike.setImageResource(
                    unlikeImg
                )
            }

            setView()
        }
    }

    private fun setView() {
        binding.ivLike.click {//点赞
            if (context != null) {
                CommonMutateManage.likePostComment(
                    context,
                    comment.id,
                    comment.liked,
                    comment.likeCount,
                    {
                        changeLikeState(
                            !comment.liked,
                            if (!comment.liked) comment.likeCount + 1 else comment.likeCount - 1
                        )
                    }) { like, count ->
                    comment.liked = like
                    comment.likeCount = count

                    changeLikeState(like, count)
                }
            }
        }
        binding.ivUser.click {
            comment.user?.accountF?.let { it1 ->
                MineManager.navigationToOtherUserPersonHome(
                    context,
                    it1.id,
                    it1.deleted,
                    it1.username
                )
            }
        }
    }


    private fun changeLikeState(liked: Boolean = false, likeNum: Int) {
        if (liked) {
            binding.ivLike.setImageResource(likeImg)
        } else {
            binding.ivLike.setImageResource(
                unlikeImg
            )
        }
        binding.tvLikeNum.text = "$likeNum"
    }
}